package icontrol

import org.springframework.dao.DataIntegrityViolationException

class TareaController {
    
    static beforeInterceptor = {
        if (!session.usuario) {
            redirect(controller: 'login')
            return false
        }
    }

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [tareaInstanceList: Tarea.list(params), tareaInstanceTotal: Tarea.count()]
    }

    def create() {
        def proyecto = Proyecto.get(params.id)
        [tareaInstance: new Tarea(params)
        ,proyecto: proyecto]
    }

    def save() {
        def tareaInstance = new Tarea(params)
        //println(params.proyecto.id)
        if (!tareaInstance.save(flush: true)) {
            render(view: "create", model: [tareaInstance: tareaInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'tarea.label', default: 'Tarea'), tareaInstance.id])
        redirect(controller:"proyecto", action: "show", id: params.proyecto.id)
    }

    def show() {
        def tareaInstance = Tarea.get(params.id)
        if (!tareaInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'tarea.label', default: 'Tarea'), params.id])
            redirect(action: "list")
            return
        }

        [tareaInstance: tareaInstance]
    }

    def edit() {
        def tareaInstance = Tarea.get(params.id)
        def proyecto      = Proyecto.get(tareaInstance.proyecto.id)
        if (!tareaInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'tarea.label', default: 'Tarea'), params.id])
            redirect(action: "list")
            return
        }
        
        [tareaInstance: tareaInstance, proyecto: proyecto]
    }

    def update() {
        def tareaInstance = Tarea.get(params.id)
        if (!tareaInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'tarea.label', default: 'Tarea'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (tareaInstance.version > version) {
                tareaInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'tarea.label', default: 'Tarea')] as Object[],
                          "Another user has updated this Tarea while you were editing")
                render(view: "edit", model: [tareaInstance: tareaInstance])
                return
            }
        }

        tareaInstance.properties = params

        if (!tareaInstance.save(flush: true)) {
            render(view: "edit", model: [tareaInstance: tareaInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'tarea.label', default: 'Tarea'), tareaInstance.id])
        redirect(action: "show", id: tareaInstance.id)
    }

    def delete() {
        def tareaInstance = Tarea.get(params.id)
        if (!tareaInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'tarea.label', default: 'Tarea'), params.id])
            redirect(action: "list")
            return
        }

        try {
            tareaInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'tarea.label', default: 'Tarea'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'tarea.label', default: 'Tarea'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
    
    def tomarTarea() {
        def tareaInstance = Tarea.get(params.id)
        tareaInstance.estado = Estado.get(2)
        tareaInstance.save()
        redirect(action: "edit", id: params.id)
    }
    
    def terminarTarea() {
        println('terminando...')
        def tareaInstance = Tarea.get(params.id)
        tareaInstance.estado = Estado.get(3)
        tareaInstance.save()
        redirect(action: "edit", id: params.id)
    }
    
    def seleccionarTareasPadre() {
        def proyecto = Proyecto.get(params.proyecto)
        
        def tareas = Tarea.withCriteria {
            like('descripcion', '%' + params.q + '%')
        }
        
        render(view: "seleccionarTareaPadre", model: [tareas: tareas])
    }
    
    def tareasAjax(){
        def proyecto = Proyecto.get(params.proyecto)
        def tareas = Tarea.withCriteria {
            like('descripcion', '%' + params.q + '%')
        }
        
        def grilla = "<table width=''> " +
            "<thead>" +
                "<tr>" +
                "<th>Nombre</th>" +
                "<th>Descripción</th>" +
                "<th>Fecha Inicio</th>" +
                "<th>Fecha Término</th>" +
                "<th></th>" +
                "</tr>" +
            "</thead>" +
            "<tbody>"
        for(int i = 0;i < tareas.size();i ++){
                grilla += "<tr>" +
                    "<td width=''>" + tareas[i].getNombre() + "</td>" +
                    "<td width=''>" + tareas[i].getDescripcion() + "</td>" +
                    "<td width=''>" + tareas[i].getFechaInicio() + "</td>" +
                    "<td width=''>" + tareas[i].getFechaTermino() + "</td>" +
                    "<td width=''><a href='javascript:seleccionarTareaPadreInput(\"" + tareas[i].getNombre() + "\", " + tareas[i].getId() + ");'>seleccionar</a></td>" +
                "</tr>" +
            "</tbody>" +
            "</table>"
        }
        
        render grilla
    }
}
